package com.xcexample.electronicclasscard.log;


import android.os.Environment;

import java.io.File;
import java.io.IOException;

public class FileHelp {

    /**
     * 获取SD卡根目录路径
     *
     * @return
     */
    public static String getSdCardPath() {
        boolean exist = isSdCardExist();
        String sdpath = "";
        if (exist) {
            sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
            sdpath = null;
        }
        return sdpath;

    }

    /**
     * 判断SDCard是否存在 [当没有外挂SD卡时，内置ROM也被识别为存在sd卡]
     *
     * @return
     */
    public static boolean isSdCardExist() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    //判断文件是否存在
    public boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }

        } catch (Exception e) {
            return false;
        }

        return true;
    }

    /**
     * 创建目录
     *
     * @param saveDir 文件目录
     * @return 绝对路径
     * @throws IOException
     */
    public static String isExistDir(String saveDir) {
        String savePath = null;
        try {
            // 下载位置
            File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
            if (!downloadFile.mkdirs()) {
                downloadFile.createNewFile();
            }
            savePath = downloadFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return savePath;
    }

}
